package com.ikingtech.platform.service.application.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ikingtech.framework.sdk.application.model.ApplicationQueryParamDTO;
import com.ikingtech.framework.sdk.base.model.PageParam;
import com.ikingtech.framework.sdk.base.model.PageResult;
import com.ikingtech.framework.sdk.enums.application.ApplicationStatusEnum;
import com.ikingtech.framework.sdk.utils.Tools;
import com.ikingtech.platform.service.application.entity.ApplicationDO;
import com.ikingtech.platform.service.application.mapper.ApplicationMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author tie yan
 */
@Service
public class ApplicationService extends ServiceImpl<ApplicationMapper, ApplicationDO> {

    public PageResult<ApplicationDO> listPage(ApplicationQueryParamDTO queryParam) {
        return PageResult.build(this.page(new Page<>(queryParam.getPage(), queryParam.getRows()), Wrappers.<ApplicationDO>lambdaQuery()
                .in(Tools.Coll.isNotBlank(queryParam.getCodes()), ApplicationDO::getCode, queryParam.getCodes())
                .in(Tools.Coll.isNotBlank(queryParam.getTypes()), ApplicationDO::getType, queryParam.getTypes())
                .like(Tools.Str.isNotBlank(queryParam.getName()), ApplicationDO::getName, queryParam.getName())
                .eq(Tools.Str.isNotBlank(queryParam.getTenantCode()), ApplicationDO::getTenantCode, queryParam.getTenantCode())
                .eq(Tools.Str.isNotBlank(queryParam.getStatus()), ApplicationDO::getStatus, queryParam.getStatus())
                .like(Tools.Str.isNotBlank(queryParam.getCreateName()), ApplicationDO::getCreateName, queryParam.getCreateName())
                .like(Tools.Str.isNotBlank(queryParam.getRemark()), ApplicationDO::getRemark, queryParam.getRemark())
                .orderByDesc(ApplicationDO::getCreateTime)));
    }

    public List<ApplicationDO> listPublishedApplication() {
        return this.list(Wrappers.<ApplicationDO>lambdaQuery()
                .eq(ApplicationDO::getStatus, ApplicationStatusEnum.PUBLISHED.name()));
    }

    public PageResult<ApplicationDO> pagePublishedApplication(PageParam param) {
        return PageResult.build(this.page(new Page<>(param.getPage(), param.getRows()), Wrappers.<ApplicationDO>lambdaQuery()
                .eq(ApplicationDO::getStatus, ApplicationStatusEnum.PUBLISHED.name())));
    }

    public boolean existByCodes(List<String> codes) {
        return this.baseMapper.exists(Wrappers.<ApplicationDO>lambdaQuery().in(ApplicationDO::getCode, codes));
    }
}
